package com.lanyun.oss.interceptor;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.lanyun.core.context.LoginContextHolder;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 拦截器：gateway拦截器
 *
 * @Author Jqcc
 * @Version 1.0
 * @Data 2025/3/11
 */
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String loginId = request.getHeader(LoginContextHolder.LOGIN_USER_HEADER);
        if (StringUtils.isNotBlank(loginId)) {
            LoginContextHolder.set(LoginContextHolder.LOGIN_USER_HEADER, loginId);
        }
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        LoginContextHolder.remove();
    }
}
